package course.filter;

import course.pojo.entity.Admin;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 管理员未登录拦截
 *
 * @author HLF
 * @date 2022/7/5 14:23
 */
@WebFilter(filterName = "AdminLoginFilter",value =
        {
                "/admin/updateController/*",
                "/admin/collegeController/*",
                "/admin/studentController/*",
                "/admin/subjectController/*",
                "/admin/teacherController/*",
                "/admin/zhuanyeController/*",
                "/admin/bathExportController/*",
                "/admin_center.jsp",
                "/admin_center_update.jsp",
                "/college_add.jsp",
                "/college_manage.jsp",
                "/college_update.jsp",
                "/student_add.jsp",
                "/student_manage.jsp",
                "/student_update.jsp",
                "/subject_add.jsp",
                "/subject_manage.jsp",
                "/subject_update.jsp",
                "/zhuanye_add.jsp",
                "/zhuanye_manage.jsp",
                "/zhuanye_update.jsp",
                "/teacher_add.jsp",
                "/teacher_manage.jsp",
                "/teacher_update.jsp",
        })
public class AdminLoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;

        HttpSession session = req.getSession();
        Admin auser = (Admin) session.getAttribute("auser");
        if (auser == null) {
            req.setAttribute("roleMsg", "未登录或管理员权限不足，无法访问！");
            req.getRequestDispatcher("/422.jsp").forward(req, resp);
        } else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
